package com.itheima.train.manager.order.handler.service;

import com.itheima.train.manager.domain.Order;
import com.itheima.train.manager.order.handler.mapper.TbOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class TbOrderService {

    @Autowired(required = false)
    private TbOrderMapper tbOrderMapper ;

    // 保存订单数据
    public Order saveOrder(Order order) {

        // 查询数据
        HashMap hashMap = new HashMap() ;
        hashMap.put("userId" , order.getUserId()) ;
        hashMap.put("trainNum" , order.getTrainNum()) ;
        hashMap.put("trainRidingDate" , order.getTrainRidingDate()) ;
        Order dbOrder = tbOrderMapper.queryOrderInfo(hashMap);

        // 判断
        if(dbOrder == null) {
            tbOrderMapper.saveOrder(order);
            return order ;
        }else {
            return dbOrder ;
        }

    }

    // 就是根据id查询订单信息
    public Order queryOrderById(Long orderId) {
        return tbOrderMapper.queryOrderById(orderId);
    }

    // 就是根据id进行修改
    public void updateOrderById(Order order) {
        tbOrderMapper.updateOrderById(order);
    }

    // 查询订单的详情信息
    public Order queryOrderInfo(String userId, String trainNum, String trainRidingDate) {

        // 查询数据
        HashMap hashMap = new HashMap() ;
        hashMap.put("userId" , userId) ;
        hashMap.put("trainNum" , trainNum) ;
        hashMap.put("trainRidingDate" , trainRidingDate) ;
        Order dbOrder = tbOrderMapper.queryOrderInfo(hashMap);

        // 返回
        return dbOrder ;
    }

}
